/* !
 * elasticlunr.stopWordFilter
 * Copyright (C) @YEAR Oliver Nightingale
 * Copyright (C) @YEAR Wei Song
 */

/**
 * elasticlunr.stopWordFilter is an English language stop words filter, any words
 * contained in the stop word list will not be passed through the filter.
 *
 * This is intended to be used in the Pipeline. If the token does not pass the
 * filter then undefined will be returned.
 * Currently this StopwordFilter using dictionary to do O(1) time complexity stop word filtering.
 *
 * @module
 * @param {String} token The token to pass through the filter
 * @return {String}
 * @see elasticlunr.Pipeline
 */
export default function (elasticlunr) {
  elasticlunr.generateStopWordFilter = function (stopWords) {
    var words = stopWords.reduce(function (memo, stopWord) {
      memo[stopWord] = stopWord;
      return memo;
    }, {});

    return function (token) {
      if (token && words[token.toString()] !== token.toString()) return token;
      return null;
    };
  };
  elasticlunr.stopWordFilter = function (token) {
    if (token && elasticlunr.stopWordFilter.stopWords[token] !== true) {
      return token;
    }
    return null;
  };

  /**
   * Remove predefined stop words
   * if user want to use customized stop words, user could use this function to delete
   * all predefined stopwords.
   *
   * @return {null}
   */
  elasticlunr.clearStopWords = function () {
    elasticlunr.stopWordFilter.stopWords = {};
  };

  /**
   * Add customized stop words
   * user could use this function to add customized stop words
   *
   * @params {Array} words customized stop words
   * @return {null}
   */
  elasticlunr.addStopWords = function (words) {
    if (words == null || Array.isArray(words) === false) return;

    words.forEach(function (word) {
      elasticlunr.stopWordFilter.stopWords[word] = true;
    }, this);
  };

  /**
   * Reset to default stop words
   * user could use this function to restore default stop words
   *
   * @return {null}
   */
  elasticlunr.resetStopWords = function () {
    elasticlunr.stopWordFilter.stopWords = elasticlunr.defaultStopWords;
  };

  elasticlunr.defaultStopWords = {
    '': true,
    'a': true,
    'able': true,
    'about': true,
    'across': true,
    'after': true,
    'all': true,
    'almost': true,
    'also': true,
    'am': true,
    'among': true,
    'an': true,
    'and': true,
    'any': true,
    'are': true,
    'as': true,
    'at': true,
    'be': true,
    'because': true,
    'been': true,
    'but': true,
    'by': true,
    'can': true,
    'cannot': true,
    'could': true,
    'dear': true,
    'did': true,
    'do': true,
    'does': true,
    'either': true,
    'else': true,
    'ever': true,
    'every': true,
    'for': true,
    'from': true,
    'get': true,
    'got': true,
    'had': true,
    'has': true,
    'have': true,
    'he': true,
    'her': true,
    'hers': true,
    'him': true,
    'his': true,
    'how': true,
    'however': true,
    'i': true,
    'if': true,
    'in': true,
    'into': true,
    'is': true,
    'it': true,
    'its': true,
    'just': true,
    'least': true,
    'let': true,
    'like': true,
    'likely': true,
    'may': true,
    'me': true,
    'might': true,
    'most': true,
    'must': true,
    'my': true,
    'neither': true,
    'no': true,
    'nor': true,
    'not': true,
    'of': true,
    'off': true,
    'often': true,
    'on': true,
    'only': true,
    'or': true,
    'other': true,
    'our': true,
    'own': true,
    'rather': true,
    'said': true,
    'say': true,
    'says': true,
    'she': true,
    'should': true,
    'since': true,
    'so': true,
    'some': true,
    'than': true,
    'that': true,
    'the': true,
    'their': true,
    'them': true,
    'then': true,
    'there': true,
    'these': true,
    'they': true,
    'this': true,
    'tis': true,
    'to': true,
    'too': true,
    'twas': true,
    'us': true,
    'wants': true,
    'was': true,
    'we': true,
    'were': true,
    'what': true,
    'when': true,
    'where': true,
    'which': true,
    'while': true,
    'who': true,
    'whom': true,
    'why': true,
    'will': true,
    'with': true,
    'would': true,
    'yet': true,
    'you': true,
    'your': true
  };

  elasticlunr.stopWordFilter.stopWords = elasticlunr.defaultStopWords;

  return elasticlunr.Pipeline.registerFunction(elasticlunr.stopWordFilter, 'stopWordFilter');
};
